<template>
  <a-modal
    :title="`用户${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 20 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="账号">
          <a-input
            v-decorator="['account', { rules: [{ required: true, message: '账号不能为空' }] }]"
            placeholder="请输入账号"
          />
        </a-form-item>
        <a-form-item label="名称">
          <a-input
            v-decorator="['name', { rules: [{ required: true, message: '名称不能为空' }] }]"
            placeholder="请输入用户名称"
          />
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-decorator="passwordDecorator" placeholder="请输入用户密码" />
        </a-form-item>
        <!-- 类型 1：管理员 2：客户 -->
        <a-form-item label="手机号码">
          <a-input
            v-decorator="['phone', { rules: [{ required: true, message: '手机号码不能为空' }] }]"
            placeholder="请输入用户手机号码"
          />
        </a-form-item>
        <a-form-item label="角色">
          <a-select v-decorator="['roleId']" placeholder="请选择用户角色">
            <a-select-option v-for="it in roleList" :key="it.id" :value="Number(it.id)">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import cloneDeep from 'lodash.clonedeep'
import { cityArr } from '@/utils/ccity'
import { Cascader } from 'ant-design-vue'
import { roleByNoAuth } from '@/api/noAuth'
// 表单字段
const fields = ['account', 'name', 'phone', 'sex', 'type', 'roleId', 'studioId', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    typeList: {
      type: Array,
      default: () => [],
    },
    type:{
      type: Number,
      default: 1,
    },
    statusList: {
      type: Array,
      default: () => [],
    },
    sexList: {
      type: Array,
      default: () => [],
    },
    levelList: {
      type: Array,
      default: () => [],
    },
    roleList: {
      type: Array,
      default: () => [],
    },
    provinceArr: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      city: '',
      cityArr,
      inviteUserList: [],
      timeout: null,
      fetching: false,
      projectType: [],
    }
  },
  components: {
    'a-cascader': Cascader,
  },
  created() {
    this.getRole()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', (n) => {
      
      if(this.model){
        this.form.setFieldsValue(pick(this.model, fields))
        if (this.model.city && this.model.country && this.model.province) {
          setTimeout(() => {
            this.model &&
              this.form.setFieldsValue({ address: [this.model.country, this.model.province, this.model.city] })
          },100)
        }
        console.log(this.model.levelId , this.model.proTypeId)
        if(this.model.levelId && this.model.proTypeId){
          
          this.getLevelList(1, { proTypeId:this.model.proTypeId, levelId:this.model.levelId })
        }
      }
    })
  },
  methods: {
    handleChange(value) {
      this.city = value
    },
    handleChangeType(value) {
      this.$forceUpdate()
    },
    getRole(){
      roleByNoAuth({pageNum:1, pageSize:100})
        .then(res=>{
          console.log(res)
        })
    }
  },
  computed: {
    c_cityArr: function () {
      let provinceArr = cloneDeep(this.provinceArr)
      let queryParam = cloneDeep(this.city)

      if (queryParam) {
        let i = provinceArr.findIndex((v) => v.name == queryParam)
        return provinceArr[i].cityList
      } else {
        return []
      }
    },
    passwordDecorator: function () {
      if (this.model && this.model.id > 0) {
        return ['password']
      } else {
        return ['password', { rules: [{ required: true, message: '密码不能为空' }] }]
      }
    },
  },
  watch: {
  },
}
</script>
